package qin.com.croper;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {
    private ViewPager mViewPager;
    private int[] mImgs = new int[]{R.drawable.test, R.drawable.test,
            R.drawable.test};
    private View[] views = new View[mImgs.length];
    private List<MyImage> myImageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myImageList = new ArrayList<>();
        for (int mImg : mImgs) {
            MyImage myImage = new MyImage();
            myImage.setSrc(mImg);
            myImageList.add(myImage);
        }
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        mViewPager.setAdapter(new PagerAdapter() {

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                View view = View.inflate(MainActivity.this,R.layout.item_image,null);
                ImageView imageView = (ImageView) view.findViewById(R.id.ziv);
                MyImage myImage = myImageList.get(position);
                if (myImage.getBitmapMatrix() != null){
                    imageView.setImageMatrix(myImage.getBitmapMatrix());
                }
                imageView.setImageResource(myImage.getSrc());
                container.addView(view);
                views[position] = imageView;
                return view;
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                container.removeView((View) object);
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                return myImageList.size();
            }
        });

        mViewPager.setOffscreenPageLimit(3);
    }
}
